package org.br.erp.base.creditcheckrule.service.impl;

import org.br.erp.base.creditcheckrule.entity.Scmcreditcheckrule;
import org.br.erp.base.creditcheckrule.entity.Scmcreditcheckruleitem;
import org.br.erp.base.creditcheckrule.entity.Scmcreditcheckruleattach;
import org.br.erp.base.creditcheckrule.mapper.ScmcreditcheckruleitemMapper;
import org.br.erp.base.creditcheckrule.mapper.ScmcreditcheckruleattachMapper;
import org.br.erp.base.creditcheckrule.mapper.ScmcreditcheckruleMapper;
import org.br.erp.base.creditcheckrule.service.IScmcreditcheckruleService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 信贷检查规则
 * @Author: jeecg-boot
 * @Date:   2024-03-26
 * @Version: V1.0
 */
@Service
public class ScmcreditcheckruleServiceImpl extends ServiceImpl<ScmcreditcheckruleMapper, Scmcreditcheckrule> implements IScmcreditcheckruleService {

	@Autowired
	private ScmcreditcheckruleMapper scmcreditcheckruleMapper;
	@Autowired
	private ScmcreditcheckruleitemMapper scmcreditcheckruleitemMapper;
	@Autowired
	private ScmcreditcheckruleattachMapper scmcreditcheckruleattachMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Scmcreditcheckrule scmcreditcheckrule, List<Scmcreditcheckruleitem> scmcreditcheckruleitemList,List<Scmcreditcheckruleattach> scmcreditcheckruleattachList) {
		scmcreditcheckruleMapper.insert(scmcreditcheckrule);
		if(scmcreditcheckruleitemList!=null && scmcreditcheckruleitemList.size()>0) {
			for(Scmcreditcheckruleitem entity:scmcreditcheckruleitemList) {


				//外键设置
				entity.setParentid(scmcreditcheckrule.getId());
				scmcreditcheckruleitemMapper.insert(entity);
			}
		}
		if(scmcreditcheckruleattachList!=null && scmcreditcheckruleattachList.size()>0) {
			for(Scmcreditcheckruleattach entity:scmcreditcheckruleattachList) {
				//外键设置
				entity.setParentid(scmcreditcheckrule.getId());
				scmcreditcheckruleattachMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Scmcreditcheckrule scmcreditcheckrule,List<Scmcreditcheckruleitem> scmcreditcheckruleitemList,List<Scmcreditcheckruleattach> scmcreditcheckruleattachList) {
		scmcreditcheckruleMapper.updateById(scmcreditcheckrule);
		
		//1.先删除子表数据
		scmcreditcheckruleitemMapper.deleteByMainId(scmcreditcheckrule.getId());
		scmcreditcheckruleattachMapper.deleteByMainId(scmcreditcheckrule.getId());
		
		//2.子表数据重新插入
		if(scmcreditcheckruleitemList!=null && scmcreditcheckruleitemList.size()>0) {
			for(Scmcreditcheckruleitem entity:scmcreditcheckruleitemList) {
				//外键设置
				entity.setParentid(scmcreditcheckrule.getId());
				scmcreditcheckruleitemMapper.insert(entity);
			}
		}
		if(scmcreditcheckruleattachList!=null && scmcreditcheckruleattachList.size()>0) {
			for(Scmcreditcheckruleattach entity:scmcreditcheckruleattachList) {
				//外键设置
				entity.setParentid(scmcreditcheckrule.getId());
				scmcreditcheckruleattachMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		scmcreditcheckruleitemMapper.deleteByMainId(id);
		scmcreditcheckruleattachMapper.deleteByMainId(id);
		scmcreditcheckruleMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			scmcreditcheckruleitemMapper.deleteByMainId(id.toString());
			scmcreditcheckruleattachMapper.deleteByMainId(id.toString());
			scmcreditcheckruleMapper.deleteById(id);
		}
	}
	
}
